<template>
  <div class="kind">
    <h1>品类管理</h1>
    <div class="goodList">
      <p>当前商品分类ID:{{categoryId}}</p>
      <el-table :data="tableData" border style="width: 100%" max-height="700">
        <el-table-column fixed prop="id" label="品类ID" width="200">
        </el-table-column>
        <el-table-column prop="name" label="品类名称" width="auto">
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small"
              >修改名称</el-button
            >
            <el-button type="text" size="small"  @click="lookChild(scope.row)" v-if="categoryId==0">查看其子品类</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog
        title="修改"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose">
          <el-input v-model="changeData.categoryName"></el-input>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 分页 -->
      <!-- <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="pageData.pageNum"
          :page-size="pageData.pageSize"
          layout="prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div> -->
    </div>
  </div>
</template>

<script>
import { kind ,kindUpdata} from "../api/index"
export default {
  name: "kind",
  data() {
    return {
      currentPage3: 5,
      tableData: [],
      changeData:{
        categoryId:null,
        categoryName:""
      },
      // pageData:{
      //   pageSize:10,
      //   pageNum:1
      // },
      // total:0
      categoryId:0,
      dialogVisible:false
    };
  },
  created(){
    this.init(0)
  },
  methods: {
    init(categoryId){
      kind({categoryId}).then(res=>{
        console.log(res);
        this.tableData = res.data
      })
    },
    lookChild(row){
      console.log(row);
      this.init(row.id)
      this.categoryId = row.id
    },
    handleClick(row) {
      this.changeData.categoryName = row.name
      this.changeData.categoryId = row.id
      this.dialogVisible = true
    },
    submit(){
      kindUpdata(this.changeData).then(res=>{
        if(res.data.status ==0){
          this.$message.success(res.data)
          this.dialogVisible = false
        }else{
          this.$message.error(res.data.msg)
          this.dialogVisible = false
        }
      })
      this.init()
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    handleSizeChange(val) {
      this.pageData.pageSize = val
      this.init(this.pageData)
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.pageData.pageNum = val
      this.init(this.pageData)
      console.log(`当前页: ${val}`);
    },
  },
};
</script>

<style scoped lang="scss">
</style>